package com.jpsl.core.reflection.property.m;

import com.jpsl.core.reflection.property.AccessException;
import com.jpsl.core.reflection.property.PropertyValueSetter;

import java.lang.reflect.Method;

/**
 * @author 懒王猫
 */
class ByMethodPropertyValueSetter implements PropertyValueSetter {

    // not method name
    private final String propertyName;

    private final Class<?> parameterType;

    private final Method setter;

    ByMethodPropertyValueSetter(String propertyName, Class<?> parameterType, Method setter) {
        this.propertyName = propertyName;
        this.parameterType = parameterType;
        this.setter = setter;
        this.setter.setAccessible(true);
    }

    @Override
    public String getName() {
        return propertyName;
    }

    @Override
    public Class<?> getType() {
        return parameterType;
    }

    @Override
    public void setValue(Object bean, Object value) throws AccessException {
        try {
            setter.invoke(bean, value);
        } catch (ReflectiveOperationException e) {
            throw new AccessException(e);
        }
    }

}
